package com.zxy.supplier_system.server.entity

import jakarta.persistence.*
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import java.io.Serializable
import java.math.BigDecimal


@Entity
@IdClass(MarketSaleDocumentItem.UnionId::class)
@DynamicUpdate
@DynamicInsert
class MarketSaleDocumentItem {
    data class UnionId(
        var goods: Int = 0,
        var document: Long = 0
    ) : Serializable

    @ManyToOne(optional = false)
    @JoinColumn
    @Id
    lateinit var goods: Goods

    @Column(nullable = false)
    var amount: Int = 0

    @ManyToOne(optional = false)
    @JoinColumn(nullable = false)
    @Id
    lateinit var document: MarketSaleDocument

    var sequence: Int = 0

    lateinit var totalFee: BigDecimal

}

val List<MarketSaleDocumentItem>.nextSequence: Int
    get() = this.maxOfOrNull {
        it.sequence
    }?.plus(1) ?: 0